package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConstant;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.QueryPageBean;
import com.itheima.pojo.Result;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @author: yongjichu
 * @date: 2020/1/11 11:26
 * @description:
 */
@RequestMapping("/checkgroup")
@RestController
public class CheckGroupController {
    @Reference(cluster = "failfast")
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @param checkGroup
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.add(checkGroup);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 检查组的分页
     * @param queryPageBean
     * @return
     */
    @RequestMapping("queryPage")
    public PageResult queryPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult=checkGroupService.queryPage(queryPageBean);
            return new PageResult(pageResult.getTotal(),pageResult.getRows(),MessageConstant.QUERY_CHECKGROUP_SUCCESS,true);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(MessageConstant.QUERY_CHECKGROUP_FAIL,false);
        }
    }

    /**
     * 根据id查出检查组的信息，检查项的所有信息和用户选择的检查项的id，用于回显页面
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map=checkGroupService.findById(id);
            return Result.success(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.edit(checkGroup);
            return Result.success(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

    }

    /**
     * 查询所有的检查组信息
     * @return
     */
    @RequestMapping("/findAllTwo")
    public Result findAllTwo(){
        try {
            List<CheckGroup> checkGroupList=checkGroupService.findAllTwo();
            return Result.success(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
